package com.uchengwang.ui.cm_ui.cycle.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWhitTitleNoBackActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleThemeBean;
import com.uchengwang.ui.utils.DialogUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/5/26.
 */

public class EditInfoCreateCycleActivity extends BaseWhitTitleNoBackActivity implements View.OnClickListener{
    private RelativeLayout edit_scope_rl, edit_theme_rl, edit_name_rl, edit_sign_rl;
    private TextView edit_scope, edit_theme, edit_name, edit_sign;
    private ImageView edit_head;
    private Button edit_next;

    private String scopeString, themeString, nameString, signString, inviteCodeString, headUrl ;
    private CycleThemeBean cycleThemeBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

        inviteCodeString = getIntent().getStringExtra(BaseConfig.INVITE_CODE);
        setContentView(R.layout.activity_edit_info_create_cycle);

        initView();
    }

    private void initView() {
        edit_scope_rl = (RelativeLayout) findViewById(R.id.edit_scope_rl);
        edit_scope_rl.setOnClickListener(this);

        edit_theme_rl = (RelativeLayout) findViewById(R.id.edit_theme_rl);
        edit_theme_rl.setOnClickListener(this);

        edit_name_rl = (RelativeLayout) findViewById(R.id.edit_name_rl);
        edit_name_rl.setOnClickListener(this);

        edit_sign_rl = (RelativeLayout) findViewById(R.id.edit_sign_rl);
        edit_sign_rl.setOnClickListener(this);

        edit_scope = (TextView) findViewById(R.id.edit_scope);
        edit_theme = (TextView) findViewById(R.id.edit_theme);
        edit_name = (TextView) findViewById(R.id.edit_name);
        edit_sign = (TextView) findViewById(R.id.edit_sign);
        edit_head = (ImageView) findViewById(R.id.edit_head);
        edit_head.setOnClickListener(this);

        edit_next = (Button) findViewById(R.id.edit_next);
        edit_next.setOnClickListener(this);

        initData();
    }

    private void initData(){
        edit_scope.setText(UChengApplication.mySchoolName);
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.edit_scope_rl://选择区域

                break;
            case R.id.edit_theme_rl://选择主题
                Intent in = new Intent(this, CycleThemesActivity.class);
                startActivityForResult(in, 0);
                break;
            case R.id.edit_name_rl://输入社圈名字
                DialogUtils.getUtils().showEditWindow(this, "社圈名称", nameString, 15, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (!TextUtils.isEmpty(data)){
                            nameString = data;
                            edit_name.setText(data);
                        }
                    }
                });

                break;
            case R.id.edit_sign_rl://输入社圈标语
                DialogUtils.getUtils().showEditWindow(this, "社圈标语",  signString, 50, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (!TextUtils.isEmpty(data)){
                            signString = data;
                            edit_sign.setText(data);
                        }
                    }
                });
                break;
            case R.id.edit_head://选择头像
                chooseImage();
                break;

            case R.id.edit_next://下一步
                if (cycleThemeBean == null){
                    UCommonUtil.getUtil().showToast(this, "请选择主题");
                    return;
                }
                if (TextUtils.isEmpty(nameString)){
                    UCommonUtil.getUtil().showToast(this, "请填写社圈名称");
                    return;
                }
                if (TextUtils.isEmpty(headUrl)){
                    UCommonUtil.getUtil().showToast(this, "请选择社圈头像");
                    return;
                }
                intent = new Intent(this, SubmitCreateCycleActivity.class);
                intent.putExtra(BaseConfig.INVITE_CODE, inviteCodeString);
                intent.putExtra(BaseConfig.BEAN, cycleThemeBean);//社圈主题

                intent.putExtra(BaseConfig.CYCLE_NAME, nameString);
                if (!TextUtils.isEmpty(signString)){
                    intent.putExtra(BaseConfig.CYCLE_SIGN, signString);
                }
                intent.putExtra(BaseConfig.CYCLE_COVER, headUrl);


                break;
        }
        if (intent != null){
            startActivity(intent);
        }
    }


    /**
     * 选择社圈图片
     * */
    private void chooseImage(){
        UCommonUtil.getUtil().openAndCrop(this, new OnResultListener() {
            @Override
            public void onResult(int stutas, String msg, int total, String data) {
                Log.d("jjf", "裁剪的地址="+data);
                headUrl = data;
                if (!TextUtils.isEmpty(headUrl)){
                    ImageManager.getInstance().loadImage(EditInfoCreateCycleActivity.this, edit_head, headUrl, ImageManager.ImageType.ROUND);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode ==0 && data != null){
            cycleThemeBean = (CycleThemeBean) data.getSerializableExtra(BaseConfig.BEAN);
            edit_theme.setText(cycleThemeBean.getItemName());

        }
    }

    //发布社圈成功 finish
    public void onEvent(String con) {
        if (!TextUtils.isEmpty(con) && con.equals(BaseConfig.RELEASE_CYCLE_SUCCESS)){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
